//
//  WelcomeController.swift
//  Weibo项目
//
//  Created by vn on 16/7/22.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    
    override func loadView() {
        
       
        view = welecomeView
        
        
    }
    
    
    //设置头像动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //从新跟新约束
        avatars_img.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(view).offset(100)
            
         }
        
        
        //动画刷新View,
        UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
                    self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
           
                
                UIView.animateKeyframesWithDuration(1, delay: 0.5, options: [], animations: { () -> Void in
                    
                        self.welecome_lb.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                      //通知完成以后切换根控制器
                      NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVC, object:"welcome")
                        
                         
                })
                
               
                
        }
        
    }
    
    func setUpUI(){
        
        //添加控件
        view.addSubview(avatars_img)
        view.addSubview(welecome_lb)
        
        //设置约束
        
        avatars_img.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
            make.width.height.equalTo(90)
            
         }
        
        
        welecome_lb.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(avatars_img)
            make.top.equalTo(avatars_img.snp_bottom).offset(16)
            
        }
        
    }
    
    
    //用户头像
    lazy var avatars_img:UIImageView = {
        
       let user_img = UIImageView()
        
       
        user_img.layer.borderColor = ThemeColor.CGColor
        user_img.layer.borderWidth = 2
        user_img.layer.cornerRadius = 45
        user_img.image = UIImage(named:"avatar_default_big")
        
        let url = OAuthViewModel.sharedViewModel.userAccount?.avatar_large
        let url2 = NSURL(string: url!)
        user_img.sd_setImageWithURL(url2)
        
        user_img.layer.masksToBounds = true
        return user_img
        
    }()
    
    //欢迎Label
    
    lazy var welecome_lb:UILabel = {
        
       let lb = UILabel()
        lb.text = "欢迎回来"
        lb.font = UIFont.systemFontOfSize(15)
        lb.alpha = 0;
        
        
        return lb
        
    }()
    
    
    lazy var welecomeView: UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    

}
